﻿#include "stdafx.h"
#include "Src/FUI_InputEvent.h"
#include "Src/FUI_FuncInterface.h"



#ifdef _DEBUG
CDllHelperAuto _dll(new CDllHelper("StrategyPlatformD.dll"));
#else
CDllHelperAuto _dll(new CDllHelper("StrategyPlatform.dll"));
#endif

#ifdef _DEBUG
CDllHelperAuto _dllPlusA(new CDllHelper("cdwStrategyD.dll"));
#else
CDllHelperAuto _dllPlusA(new CDllHelper("cdwStrategy.dll"));
#endif



typedef IStrategyPlatform* (*CreateStrategyPlatform)();
CreateStrategyPlatform _func = NULL;
IStrategyPlatform * _pPlatForm = NULL;
typedef void* (*ReleaseStrategyPlatform)();
ReleaseStrategyPlatform _func2 = NULL;

typedef IStrategyPlus* (*CreateStrategyCDW)(IStrategyPlatform*p,ICDW*pCDW);
CreateStrategyCDW _funcA = NULL;
IStrategyPlus * _pStrategyPlusA = NULL;
typedef void* (*ReleaseStrategyCDW)();
ReleaseStrategyCDW _funcA2 = NULL;


#ifdef _DEBUG
CDllHelperAuto _dllPrint(new CDllHelper("TPrintD.dll"));
#else
CDllHelperAuto _dllPrint(new CDllHelper("TPrint.dll"));
#endif

typedef TPRINT::IYKPrint* (*CreatePrint)();
typedef void* (*ReleasePrint)();

CreatePrint _print = NULL;
ReleasePrint _printDel = NULL;
TPRINT::IYKPrint * m_print;


createDeviceApp::createDeviceApp()
:szImei("123456789012345")
{

}

bool createDeviceApp::Create()
{
	//DebugBreak();

	FUI_INIT it;
	it.hwnd  = fge->GetHwnd();
	it.wnd_w = fge->GetWidthF();
	it.wnd_h = fge->GetHeightF();
	it.base_dir = FAppGetAssetsRootA();
	it.userPTR  = (DWORD_PTR)fge;
	FUI_InitFuncInterface(it);
	uiSys.Initialize(_FTA("cdw.fud"), &it);
	myWnd.LoadUI(&uiSys);

	/************************************************************************/
	/* 
		init openctp
	*/
	/************************************************************************/
	

	//init	

	//创建多线程打印
	_print = _dllPrint->GetProcedure<CreatePrint>("CreateObject");
	_printDel = _dllPrint->GetProcedure<ReleasePrint>("ReleaseObject");
	if (_print)
	{
		m_print = _print();
	}

	/*
	Platform
	*/

	_func = _dll->GetProcedure<CreateStrategyPlatform>("CreateObject");
	if (_func)
	{
		_pPlatForm = _func();
		printf("Init StrategyPlatform\n");
	}
	int ret = _pPlatForm->Init(m_print);
	if (ret != SUCCESS)
	{
		printf("Init StrategyPlatform Fail\n");
		return false;
	}
	//_func2 = _dll->GetProcedure<ReleaseStrategyPlatform>("ReleaseObject");

	/************************************************************************/
	/* 
		启动cdw
	*/
	/************************************************************************/

#ifdef _DEBUG
	LPSTRATEGYPLUS cdwStrategy = _pPlatForm->GetStrategy("cdwStrategyD");

#else
	LPSTRATEGYPLUS cdwStrategy = _pPlatForm->GetStrategy("cdwStrategy");

#endif // DEBUG

	cdwStrategy->SetHandle(this);

	return true;
}
void createDeviceApp::Destroy()
{
	myWnd.UnLoad();
	uiSys.Release();	
}
void createDeviceApp::Render()
{
    // 对渲染逻辑进行性能统计，性能日志存在log\[date-time]\profile - [app_name].log中
	FPF_FuncAuto;	    
	uiSys.Render();
	uiSys.RenderTips();
}
bool createDeviceApp::OnInput(FINPUT_PACKAGE& ipk)
{
	static bool is_down = false;
	switch (ipk.nType)
	{
	case eIT_KEYUP:
		if (ipk.p1 == VK_ESCAPE)
			fge->Quit();
		else if (ipk.p1 == 'A')
			fge->GetFont()->__OutputFntTex(eCD_DefFontID, _FTA("d:\\aaa.png"));
		break;
	case eIT_LBUTTON_DOWN:
		sprx = ipk.p2.v.v2.x;
		spry = ipk.p2.v.v2.y;
		is_down = true;
		break;
	case eIT_MOUSEMOVE:
		if (is_down)
		{
			sprx = ipk.p2.v.v2.x;
			spry = ipk.p2.v.v2.y;
		}
		break;
	case eIT_LBUTTON_UP:
		is_down = false;
		sprx = ipk.p2.v.v2.x;
		spry = ipk.p2.v.v2.y;
		break;
	}
	return FInputTransmitToUI(ipk, fge, &uiSys);
	return false;
}
	
void createDeviceApp::Update()
{
	uiSys.Update();
}

void createDeviceApp::OnQuote(tagMarketData * data)
{
	myWnd.SetQuote(data);
}
